package com.rtf.framework.common.util;

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * 通过反射机制获取对象的属性
 * @Author : liupeng
 * @Date : 2019-01-05
 * @Modified By
 */
public class UtilBeanProperty extends PropertyUtils {

    /**
     * 设置对象属性
     * @param bean
     * @param field
     * @param value
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static void setProperty(final Object bean, Field field, final Object value)
            throws IllegalAccessException, InvocationTargetException {
        try{
            PropertyUtils.setProperty( bean , field.getName() , value );
        }catch( NoSuchMethodException e ){
            field.setAccessible(true);
            try {
                field.set( bean , value ) ;
            } catch (IllegalAccessException ex) {
                throw ex ;
            }
        }
    }

}
